import G6, {Graph, IEdge, INode} from '@antv/g6';
import {G6Event} from '@antv/g6-core/lib/types';
import NodeStateType from '../consts/node-state-type';
import GraphEventName from '../consts/graph-event-name';
import StateType from '../consts/state-type';

/**
 * 显示拖动时的阴影
 */
export default function () {
    G6.registerBehavior('custom:items:set-select', {
        getDefaultCfg() {
            return {

            };
        },
        getEvents() {
            return {
                'custom:items:set-select': 'setItemsSelect',
                'custom:items:clear-select': 'clearItemsSelect',
            } as {
                [key in G6Event]?: string;
            };
        },
        setItemsSelect(items: (INode | IEdge)[]) {
            // @ts-ignore
            this.setSelect(items, true);
        },
        clearItemsSelect(items: (INode | IEdge)[]) {
            // @ts-ignore
            this.setSelect(items, false);
            const graph = this.graph as Graph;
            graph.emit(GraphEventName.GRAPH_SELECT_CLEARED);
        },
        setSelect(items: (INode | IEdge)[], selected: boolean = true) {

            const graph = this.graph as Graph;
            if (items === undefined) {
                items = [...graph.getNodes(), ...graph.getEdges()];
            }
            items.forEach((item: INode | IEdge) => {
                item.setState(StateType.SELECTED, selected);
            });
        }
    });
}
